<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>采购订单API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .test-button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }

        .test-button:hover {
            background: #0056b3;
        }

        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: monospace;
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
    </style>
</head>

<body>
    <h1>采购订单API测试</h1>

    <div class="test-section">
        <h3>1. 测试获取采购订单列表</h3>
        <button class="test-button" onclick="testGetList()">获取列表</button>
        <div id="listResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>2. 测试创建采购订单</h3>
        <button class="test-button" onclick="testCreate()">创建订单</button>
        <div id="createResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>3. 测试获取单个订单</h3>
        <input type="number" id="orderId" placeholder="输入订单ID" value="1" style="width: 100px;">
        <button class="test-button" onclick="testGetById()">获取详情</button>
        <div id="getByIdResult" class="result"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5000';

        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.textContent = JSON.stringify(data, null, 2);
            element.className = `result ${isError ? 'error' : 'success'}`;
        }

        async function testGetList() {
            try {
                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder/list?PageIndex=1&PageSize=10`);
                const data = await response.json();
                showResult('listResult', data);
            } catch (error) {
                showResult('listResult', { error: error.message }, true);
            }
        }

        async function testCreate() {
            try {
                const orderData = {
                    orderSubject: "测试采购订单",
                    isSystemNumber: true,
                    supplierId: 1,
                    purchaseDate: new Date().toISOString().split('T')[0],
                    details: [
                        {
                            serialNumber: 1,
                            itemNumber: "TEST001",
                            itemName: "测试物品",
                            purchaseQuantity: 10,
                            mainUnitQuantity: 10,
                            taxRate: 13,
                            unitPriceIncludingTax: 100,
                            unitPriceExcludingTax: 88.5
                        }
                    ]
                };

                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(orderData)
                });

                const data = await response.json();
                showResult('createResult', data);
            } catch (error) {
                showResult('createResult', { error: error.message }, true);
            }
        }

        async function testGetById() {
            try {
                const orderId = document.getElementById('orderId').value;
                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder/${orderId}`);
                const data = await response.json();
                showResult('getByIdResult', data);
            } catch (error) {
                showResult('getByIdResult', { error: error.message }, true);
            }
        }

        // 页面加载时自动测试列表API
        window.onload = function () {
            testGetList();
        };
    </script>
</body>

</html>